/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.adapter;

import net.cachapa.libra.R;
import net.cachapa.libra.util.Util;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Gallery.LayoutParams;

public class ValueSelectorAdapter extends BaseAdapter {
	public static final int TYPE_GENERIC = 0;
	public static final int TYPE_HEIGHT_IMPERIAL = 1;
	public static final int TYPE_WEIGHT_STONES = 2;
	
	private Context context;
	private int minValue;
	private int maxValue;
	private int multiplyer;
	private int type;
	
	public ValueSelectorAdapter(Context context, int minValue, int maxValue, int decimalPlaces, int type) {
		this.context = context;
		this.minValue = minValue;
		this.maxValue = maxValue;
		this.multiplyer = (int) Math.pow(10, decimalPlaces);
		this.type = type;
	}
	
	public ValueSelectorAdapter(Context context, int minValue, int maxValue, int decimalPlaces) {
		this(context, minValue, maxValue, decimalPlaces, TYPE_GENERIC);
	}

	public int getCount() {
		return (maxValue - minValue) * multiplyer;
	}
	
	public int getPosition(float value) {
		return (int) Util.Round(((value - minValue) * multiplyer), 0);
	}
	
	public float getValue(long id) {
		return (float)(id + (minValue * multiplyer)) / multiplyer;
	}

	/**
	 * Not used. Use getValue instead.
	 */
	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		switch (type) {
		case TYPE_HEIGHT_IMPERIAL:
			view = generateImperialHeightTick(position);
			break;
		case TYPE_WEIGHT_STONES:
			view = generateStonesTick(position);
			break;
		default:
			view = generateTick(position);
		}
		view.setLayoutParams(new Gallery.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));		
		return view;
	}
	
	private View generateTick(int position) {
		float value = (float)(minValue * multiplyer + position) / multiplyer;
		
		View view;
		if (position % multiplyer == 0) {
			view = LayoutInflater.from(context).inflate(R.layout.valueselectoritem_large, null);
			TextView textView = (TextView) view.findViewById(R.id.Value);
			textView.setText((int)value + "");	
		}
		else {
			view = LayoutInflater.from(context).inflate(R.layout.valueselectoritem_medium, null);
			TextView textView = (TextView) view.findViewById(R.id.Value);
			textView.setText(value + "");
		}
		
		return view;
	}
	
	private View generateImperialHeightTick(int position) {
		int feet = (minValue + position) / 12;
		int inches = (minValue + position) % 12;
		
		View view = LayoutInflater.from(context).inflate(R.layout.valueselectoritem_medium, null);
		
		TextView textView = (TextView) view.findViewById(R.id.Value);
		textView.setText(feet + "'" + inches + "''");
		
		return view;
	}
	
	private View generateStonesTick(int position) {
		float weight = getValue(position);
		int stones = (int) (weight / 14);
		float pounds = Util.Round(weight % 14, 1);
		
		View view = LayoutInflater.from(context).inflate(R.layout.valueselectoritem_stones, null);
		
		TextView textView = (TextView) view.findViewById(R.id.ValueStones);
		textView.setText(stones + "st");
		
		textView = (TextView) view.findViewById(R.id.ValuePounds);
		textView.setText(String.valueOf(pounds));
		
		return view;
	}
}